package cn.web.management.dao.impl;

import cn.web.management.dao.inface.UpdateUserDao;
import cn.web.management.domain.PersonalInform;
import cn.web.management.domain.Salary;
import cn.web.management.utils.JDBCUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;

public class UpdateUserDaoImpl implements UpdateUserDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtil.getDataSource());
    
    /**
     * @description: 注册用户时添加默认用户信息
     * @paramName: [employno, username, password]
     * @paramType: [java.lang.String, java.lang.String, java.lang.String]
     * @returnType: int
     */
    public int addUser(String employno, String username, String password){
        String sql = "insert into personal_inform values(?,'G00000',?,?,'user','default','张三','男','1970-01-01','未知','未知','未知','正常',0,'未知','未知','未知','未知','梦联动','未知','1970-01-01')";
        int count = template.update(sql,employno,username,password);
        return count;
    }

    /**
     * @description: 完整的添加用户信息
     * @paramName: [p]
     * @paramType: [cn.web.management.domain.PersonalInform]
     * @returnType: boolean
     */
    public boolean addInform(PersonalInform p) {
        String sql = "insert into personal_inform values('"+
                p.getEmployno()+"','"+
                p.getGroupno()+"','"+
                p.getUsername()+"','"+
                p.getUserpassword()+"','"+
                p.getId_card()+"','"+
                p.getUserPortrait()+"','"+
                p.getEname()+"','"+
                p.getGender()+"','"+
                p.getBirthday()+"','"+
                p.getNative_place()+"','"+
                p.getNational()+"','"+
                p.getPolitical()+"','"+
                p.getHealth()+"',"+
                p.getWorkingtime()+",'"+
                p.getId_card()+"','"+
                p.getTel()+"','"+
                p.getSchool_major()+"','"+
                p.getDegree()+"','"+
                p.getCurrent_employer()+"','"+
                p.getCurrent_position()+"','"+
                p.getEmployment_time()+"')";
        int count = template.update(sql);
        if (count==1)
            return true;
        else
            return false;
    }

    /**
     * @description: 添加工资单
     * @paramName: [salary]
     * @paramType: [cn.web.management.domain.Salary]
     * @returnType: boolean
     */
    public boolean addPayroll(Salary salary) {
        String sql = "insert into salary value " + salary.toString();
        int update = template.update(sql);
        if (update==1)
            return true;
        else
            return false;
    }

    /**
     * @description: 完整的更新用户信息
     * @paramName: [p]
     * @paramType: [cn.web.management.domain.PersonalInform]
     * @returnType: boolean
     */
    public boolean updateInform(PersonalInform p) {
        try{

        String sql = "update personal_inform set " +
                "employno = '"+p.getEmployno()+"',"+
                "groupno = '"+p.getGroupno()+"',"+
                "username = '"+p.getUsername()+"',"+
                "userpassword = '"+p.getUserpassword()+"',"+
                "userPortrait = '"+p.getUserPortrait()+"',"+
                "ename = '"+p.getEname()+"',"+
                "gender = '"+p.getGender()+"',"+
                "birthday = '"+p.getBirthday()+"',"+
                "native_place = '"+p.getNative_place()+"',"+
                "national = '"+p.getNational()+"',"+
                "political = '"+p.getPolitical()+"',"+
                "health = '"+p.getHealth()+"',"+
                "workingtime = '"+p.getWorkingtime()+"',"+
                "id_card = '"+p.getId_card()+"',"+
                "tel = '"+p.getTel()+"',"+
                "school_major = '"+p.getSchool_major()+"',"+
                "degree = '"+p.getDegree()+"',"+
                "current_employer = '"+p.getCurrent_employer()+"',"+
                "current_position= '"+p.getCurrent_position()+"',"+
                "employment_time = '"+p.getEmployment_time()+"' "+
                "where employno = '"+p.getEmployno()+"'";
        int count = template.update(sql);
        if (count==1)
            return true;
        else
            return false;
        }catch (DataIntegrityViolationException e){
            return false;
        }
    }

    /**
     * @description: 管理员更新部分的个人信息
     * @paramName: [employno, field, value]
     * @paramType: [java.lang.String, java.lang.String, java.lang.String]
     * @returnType: boolean
     */
    public boolean updatePartPersonalInform(String employno, String field, String value) {
        String sql = "update personal_inform set "+ field +" = ? where employno = ?";

        try {
        int update = template.update(sql, value, employno);
        if (update==1)
            return true;
        else
            return false;
        }catch (DataAccessException e){
            //捕获语句执行错误异常，如输入变量的不合法
            return false;
        }
    }

    /**
     * @description: 删除员工的薪水记录
     * @paramName: [employno]
     * @paramType: [java.lang.String]
     * @returnType: int
     */
    public int deleteSalary(String employno) {
        String sql = "delete from salary where employno = ?";
        int update = template.update(sql,employno);
        return update;
    }

    /**
     * @description: 删除一位用户/员工信息
     * @paramName: [employno]
     * @paramType: [java.lang.String]
     * @returnType: boolean
     */
    public boolean deletePersonalInform(String employno) {
        String sql = "delete from personal_inform where employno = ?";
        int count = template.update(sql,employno);
        if (count==1)
            return true;
        else
            return false;
    }
}
